package top.cmnbgy.springbootibatismulidatasource.dataSource;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * 从数据源配置
 * @ClassName SlaveDataSource
 * @Cescription TODO
 * @Author .朱孝辉
 * @Blog http://www.cmnbgy.top
 * @Datae 2018/11/14 0014 13:46
 **/

@Configuration
@MapperScan(basePackages = "top.cmnbgy.springbootibatismulidatasource.mapper.slave", sqlSessionTemplateRef  = "slave-SqlSessionTemplate")
@ConfigurationProperties(prefix = "spring.datasource.slave")
public class SlaveDataSource {

    private String url;
    private String username;
    private String password;
    private String driverClassName;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getDriverClassName() {
        return driverClassName;
    }

    public void setDriverClassName(String driverClassName) {
        this.driverClassName = driverClassName;
    }

    /**
     * 创建主数据源
     * @return
     */
    @Bean(name = "slave-DataSource")
    // @ConfigurationProperties(prefix = "spring.datasource.master")
    public DataSource testDataSource() {
        /**
         *  @Bean：创建bean组件，可设置bean的name，如果不写name默认为方法名
         *  @Primary：设置为优先注入，如果Bean类型有多个，设置@Primary后会将该Bean优先注入，否则会报错
         *  @ConfigurationProperties：读取application.properties 属性封装成实体类
         *
         *  DataSourceBuilder.create().build() ：SpringBoot为我们提供了简便的创建数据源方式
         */
        System.out.println("================================"+url);
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(url);
        dataSource.setUsername(username);// 用户名
        dataSource.setPassword(password);// 密码
        dataSource.setDriverClassName(driverClassName);
        return dataSource;
    }

    /**
     *      创建SqlSession工厂
     */
    @Bean(name = "slave-SqlSessionFactory")
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("slave-DataSource") DataSource dataSource) throws Exception {
        /**
         * @Qualifier：
         */
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/slave/*/*.xml"));
        return bean.getObject();
    }

    /**
     *  事务管理
     */
    @Bean(name="slave-TransactionManager")
    public DataSourceTransactionManager masterTransactionManager(@Qualifier("slave-DataSource") DataSource dataSource){
        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(dataSource);
        return dataSourceTransactionManager;
    }

    /**
     *  配置sqlsession模板
     */
    @Bean("slave-SqlSessionTemplate")
    public SqlSessionTemplate masterSQLSessionTemplate(@Qualifier("slave-SqlSessionFactory") SqlSessionFactory sqlSessionFactory){
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
